<?php
/**
 * Created by PhpStorm.
 * User: chenq
 * Date: 2018/1/2
 * Time: 下午7:31
 */

namespace app\spider\controller;
use think\Controller;
use think\Db;
use think\Exception;
use think\Log;

class DoController extends Controller
{

    private $token   = null;
    private $addUrl  = 'http://www.daaazi.com/admin/product/knowledge/save?debugAuth=1';
    private $passUrl = 'http://www.daaazi.com/admin/product/publish?debugAuth=1&productId=';
    private $requestId = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->requestId = date('YmdHis').md5(rand(100000,999999).rand(100000,999999));
        $this->getToken();//加入缓存
    }


    /**
     * @param string $logStr
     * @param string $type
     */
    public function writeLog($logStr, $type = 'log') {
        Log::write("[RID]: ".$this->requestId."; ".$logStr, $type);
    }

    public function test(){
        dump($_SERVER);
    }

    private function get($url,$header=[]){
        $timeout = 60;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//注意链接是否重定向,使用自动跳转
        curl_setopt($ch, CURLOPT_ENCODING, "");//若获得的是乱码,考虑是否是服务器开启gzip的原因 等价于 file_get_contents("compress.zlib://".$url);

        $fileContents = curl_exec($ch);
        return $fileContents;


    }


    public function doPost($url,$data){
        //签名等操作
        return $this->_post($url,$data);
    }

    private function _post($url,$data){

        $header = ['Content-Type: application/json'];
        if($this->token) $header[] = 'token: '.$this->token;
        $data = json_encode($data);

        $this->writeLog('ToPost:'.$data);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        curl_close($ch);


        $this->writeLog('ReturnPost:'.$data);

        return $result;

    }

    //一次请求token不变,具体看需求调整
    private function getToken(){

        if($this->token) return $this->token;
        $url  = 'http://www.daaazi.com/admin/admin/login';
        $data = [
            "userName"=> "newsAdmin",
            "password"=> "f379eaf3c831b04de153469d1bec345e"
        ];
        //签名等操作
        $ret  = $this->_post($url,$data);
        /***
        {
        "success": true,
        "code": "",
        "message": "",
        "data": {
        "token": "820F4221A33D4723A2A835C3DA72F089",
        "id": "2100000020",
        "type": {
        "name": "MANAGER",
        "rolesStringArray": [
        "VIEW_INTELLIGENT",
        "EDIT_INTELLIGENT",
        "DELETE_INTELLIGENT"
        ]
        }
        }
        }
         */
        $ret    = json_decode($ret,true);
        $this->token  =  (isset($ret['token']) && $ret['token'] ) ? $ret['token'] : null;

        return $this->token;

    }

    /********************** 财联社 *******************************/
    public function cailianpress(){

        $content = file_get_contents('https://www.cailianpress.com/index');
        file_put_contents('./test.txt',$content);
//      $content = file_get_contents('./test.txt');

        $reg = '/\{\"props\".*/';
        preg_match($reg,$content,$matches);
        $data = json_decode($matches[0],true);

        $list = @$data['props']['initialState']['telegraph']['telegraphList'] ? : [];


        $num = 0;
        foreach($list as $k => $v){
            $toPost['title']        = strip_tags($v['content']);
            $toPost['content']      = ' ';
            $toPost['sellerId']     = '2100000013';
            $toPost['platformId']   = 'cailianpress_'.$v['id'];
//            dump($toPost);
//            continue;

            $ret = $this->doPost($this->addUrl,$toPost);
            $ret = json_decode($ret,true);

            if(isset($ret['success']) && $ret['success']){
                $passId   = $ret['data'];
                $urlPass  = $this->passUrl.$passId;

                $ret = $this->doPost($urlPass,[]);
                $ret = json_decode($ret,true);
                //{"success":true,"code":"","message":"","data":"情报发布成功"}
                if(!isset($ret['success']) || !$ret['success']){
                    $this->writeLog('数据上架失败:'.  $toPost['platformId'].':'.$ret['message']);
                }
                $num ++;
            }else{
                $this->writeLog('数据添加失败:'.  $toPost['platformId'].':'.$ret['message']);
            }

//            break;
        }

        $this->writeLog('cailianpress:本次处理数据总条数:'.count($list).';其中成功:'.$num);
    }

    /********************** 新浪实时财经  每次20条 ******************************
     抓取地址:http://live.sina.com.cn/zt/f/v/finance/globalnews1
     */


    public function livesina(){
        $content = file_get_contents('http://live.sina.com.cn/zt/f/v/finance/globalnews1');
        file_put_contents('./sinatest.txt',$content);
//        $content = file_get_contents('./sinatest.txt');

        $reg = '/data-id="(?<id>\d*)"/';
        preg_match_all($reg,$content,$matches);
        $list = [];
        if(isset($matches['id']) && $matches['id']){
            $id = @$matches['id'][count($matches['id'])-1] ? : NULL;//获取最后一个ID
            if($id){
                $url = 'http://live.sina.com.cn/zt/api/f/get/finance/globalnews1/index.htm?format=json&id='.$id.'&tag=0&pagesize=20&dire=f&dpc=1';
                $ret  = file_get_contents($url);
                $data = json_decode($ret,true);
                if(isset($data['result']['data']) && $data['result']['data']){
                    $list = $data['result']['data'];
                }

            }

        }
/**
        {
            "result": {
            "status": {
                "code": 0,
			"msg": "获取成功, succ"
		},
		"timestamp": "Sun Jun 03 14:14:26 +0800 2018",
		"pageStr": {
                "totalPage": 55,
			"pageSize": 1,
			"prePage": 1,
			"nextPage": 2,
			"firstPage": 1,
			"lastPage": 55,
			"totalNum": 55,
			"pName": "page",
			"page": 1,
			"paramStr": "http:\/\/live.sina.com.cn\/zt\/api\/app_zt\/f\/get\/finance\/globalnews1\/index.htm\/?format=json&id=726053&tag=0&pagesize=1&dire=f&"
		},
		"data": [{
                "id": "726108",
			"mid": "18440015280061334742",
			"sp_id": "152",
			"uid": "3969238805",
			"content": "【兑奖了！亿嘉和中签号出炉 共17543个】亿嘉和披露中签结果，中签号码共有17543个，每个中签号码只能认购1000股。末“4”位数：4364；末“5”位数：50097、70097、90097、30097、10097；末“6”位数：564926、764926、964926、364926、164926；末“7”位数：7470373、9470373、5470373、3470373、1470373；末“8”位数：86946006、66946006、46946006、26946006、06946006。",
			"source": "微博 weibo.com<\/a>",
			"created_at": "1528006133",
			"top_level": "0",
			"original_pic": "",
			"pic_ids": "",
			"video": "",
			"reposts": 0,
			"comments": 0,
			"attitudes": 0,
			"pmid": "0",
			"re_mid": "0",
			"re_uid": "0",
			"re_content": "",
			"re_source": "",
			"re_created_at": "0",
			"re_original_pic": "",
			"re_pic_ids": "",
			"re_video": "",
			"re_reposts": "0",
			"re_comments": "0",
			"re_attitudes": "0",
			"is_audit": "1",
			"is_launch": "0",
			"is_del": "0",
			"creator": "zhangning8",
			"mender": "",
			"created_time": "1528006134",
			"modified_time": "0",
			"notes": "",
			"user": {
                    "id": 3969238805,
				"idstr": "3969238805",
				"class": 1,
				"screen_name": "fin_图文直播",
				"name": "fin_图文直播",
				"province": "11",
				"city": "1000",
				"location": "北京",
				"description": "",
				"url": "",
				"profile_image_url": "http:\/\/tva4.sinaimg.cn\/crop.0.0.179.179.50\/ec95c715jw1ecc6o54vfcj2050050aad.jpg",
				"profile_url": "u\/3969238805",
				"domain": "",
				"weihao": "",
				"gender": "m",
				"followers_count": 2447,
				"friends_count": 3,
				"pagefriends_count": 1,
				"statuses_count": 187484,
				"favourites_count": 0,
				"created_at": "Wed Jan 08 15:47:33 +0800 2014",
				"following": false,
				"allow_all_act_msg": false,
				"geo_enabled": true,
				"verified": false,
				"verified_type": -1,
				"remark": "",
				"insecurity": {
                        "sexual_content": false
				},
				"ptype": 0,
				"allow_all_comment": true,
				"avatar_large": "http:\/\/tva4.sinaimg.cn\/crop.0.0.179.179.180\/ec95c715jw1ecc6o54vfcj2050050aad.jpg",
				"avatar_hd": "http:\/\/tva4.sinaimg.cn\/crop.0.0.179.179.1024\/ec95c715jw1ecc6o54vfcj2050050aad.jpg",
				"verified_reason": "",
				"verified_trade": "",
				"verified_reason_url": "",
				"verified_source": "",
				"verified_source_url": "",
				"follow_me": false,
				"like": false,
				"like_me": false,
				"online_status": 0,
				"bi_followers_count": 0,
				"lang": "zh-cn",
				"star": 0,
				"mbtype": 0,
				"mbrank": 0,
				"block_word": 0,
				"block_app": 0,
				"credit_score": 80,
				"user_ability": 0,
				"urank": 16,
				"story_read_state": -1,
				"vclub_member": 0
			},
			"color": "#000000",
			"base62mid": "LYc6pvg5Be6",
			"base62pmid": "",
			"base62re_mid": "",
			"pic_ids_plus": "",
			"re_pic_ids_plus": "",
			"tag": [{
                    "tag_id": "10",
				"tag_name": "A股"
			}, {
                    "tag_id": "3",
				"tag_name": "公司"
			}]
		}]
	}
}
*/


        $num = 0;
        foreach($list as $k => $v){
            $toPost['title']        = strip_tags($v['content']);
            $toPost['content']      = ' ';
            $toPost['sellerId']     = '2100000013';
            $toPost['platformId']  = 'livesina_'.$v['id'];

//            dump($toPost);
//            continue;
//            exit;
            $ret = $this->doPost($this->addUrl,$toPost);
            $ret = json_decode($ret,true);

            if(isset($ret['success']) && $ret['success']){
                $passId   = $ret['data'];
                $urlPass  = $this->passUrl.$passId;

                $ret = $this->doPost($urlPass,[]);
                $ret = json_decode($ret,true);
                //{"success":true,"code":"","message":"","data":"情报发布成功"}
                if(!isset($ret['success']) || !$ret['success']){
                   $this->writeLog('数据上架失败:'.  $toPost['platformId'].':'.$ret['message']);
                }
                $num ++;
            }else{
                $this->writeLog('数据添加失败:'.  $toPost['platformId'].':'.$ret['message']);
            }

            //            break;
        }

        $this->writeLog('livesina:本次处理数据总条数:'.count($list).';其中成功:'.$num);
    }




    /*************************** 华尔街见闻 --宏观**************************/
    //其它数据源如:区块链 A股 美股 外汇 商品 也可以通过接口实现
    public function wallstreet(){

        $content =$this->get('https://wallstreetcn.com/live/global');
        file_put_contents('./wallstreet.txt',$content);
//        $content = file_get_contents('./wallstreet.txt');

        $reg   = '/__INITIAL_COMPONENTS_STATE__ = (?<ret>.*)/';
        preg_match_all($reg,$content,$matches);
        $ret   = json_decode($matches['ret'][0],true);
        $list  = @$ret[1]['global']['items'] ? : [];//宏观数据
        //todo:以下待需求确认再更新
        $listBlockchain  = @$ret[1]['blockchain']['items'] ?:[];//区块链
        $listAStock      = @$ret[1]['aStock']['items'] ?:[];    //A股
        $listUsStock     = @$ret[1]['usStock']['items'] ?:[];   //美股
        $listForex       = @$ret[1]['forex']['items'] ?:[];     //美股
        $listCommodity   = @$ret[1]['commodity']['items'] ?:[]; //美股


        $num = 0;
        foreach($list as $k => $v){
            $toPost['title']        = strip_tags($v['content_text']);
            $toPost['content']      = ' ';
            $toPost['sellerId']     = '2100000013';
            $toPost['platformId']  = 'wallstreet_'.$v['id'];

//            dump($toPost);
//            continue;
//            exit;

            $ret = $this->doPost($this->addUrl,$toPost);
            $ret = json_decode($ret,true);

            if(isset($ret['success']) && $ret['success']){
                $passId   = $ret['data'];
                $urlPass  = $this->passUrl.$passId;

                $ret = $this->doPost($urlPass,[]);
                $ret = json_decode($ret,true);
                //{"success":true,"code":"","message":"","data":"情报发布成功"}
                if(!isset($ret['success']) || !$ret['success']){
                    $this->writeLog('数据上架失败:'.  $toPost['platformId'].':'.$ret['message']);
                }
                $num ++;
            }else{
                $this->writeLog('数据添加失败:'.  $toPost['platformId'].':'.$ret['message']);
            }
//            break;

        }
        $this->writeLog('wallstreet:本次处理数据总条数:'.count($list).';其中成功:'.$num);
    }



    /******* 爬虫总入口  *******/
    public function main(){
        set_time_limit(0);
        $this->writeLog('********spider_begin*********');
        $this->livesina();
        $this->cailianpress();
        $this->wallstreet();
        $this->writeLog('********spider_end*********');

        echo  "hello,Good job";

    }




}
